/*
 * Copyright (c) Extreme Vision Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SOPHON_BASE_H
#define SOPHON_BASE_H
#include "bm_wrapper.hpp"
#include "bmnn_utils.h"
#include "evdeploy/core/ev_common_data.h"
#include "model_handle.h"
#include "version_config.h"
#include <dlfcn.h>
#include <fstream>
#include <iostream>
#include <mutex>

namespace ev
{
#define CREATE_INFER_INS(ENGINE)                                                                                       \
    extern "C" void *create_infer_ins() { return new ENGINE; }
#define DESTROY_INFER_INS(ENGINE)                                                                                      \
    extern "C" void destroy_infer_ins(void *ins)                                                                       \
    {                                                                                                                  \
        ENGINE *t_ins = (ENGINE *)ins;                                                                                 \
        delete t_ins;                                                                                                  \
    }

    class EVBaseSophonModel
    {
      public:
        EVBaseSophonModel() {}

        virtual ~EVBaseSophonModel() { ; }

        // load model and initization
        virtual EVStatus Init(const char *net, void **config) = 0;

        // relsease source
        virtual EVStatus UnInit() = 0;

        // run optimize, like tensorrt optimize
        virtual EVStatus OptimizeModel(const char *model_name) = 0;

        // run inference
        virtual EVStatus Infer(EVModelData *in, EVModelData *out) = 0;

        virtual EVStatus SetIO(const std::vector<std::string> &inputs, const std::vector<std::string> &outputs) = 0;

      protected:
        bool m_init_flag = false;
    };

    class EVSophonModel
    {
      public:
        EVSophonModel();

        virtual ~EVSophonModel();

      protected:
        // sophon var here
        int m_dev_id{0};
        BMNNHandlePtr m_handle_ptr;
        std::shared_ptr<bmruntime::Context> m_ctx_ptr{nullptr};
        std::vector<const char *> m_net_names;
        std::shared_ptr<bmruntime::Network> m_net_ptr{nullptr};
        bool m_is_ok{false};
        ev::SophonModelHandle m_sophon_model_handle;

      protected:
        /**
         * @brief load onnx format model for ov
         * @param[in] str_model_name: onnx model file name
         * @return
         */
        void LoadBmModel(const std::string str_model_name);
    };

} // namespace ev
#endif
